package com.lcxw.hxzbapi.controller.shortvideocontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.entity.pojo.ShortVideo;
import com.lcxw.hxzbapi.entity.pojo.ShortVideoComment;
import com.lcxw.hxzbapi.entity.pojo.SvCommentLike;
import com.lcxw.hxzbapi.entity.pojo.SvLike;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.requestBean.AddSVCommentReq;
import com.lcxw.hxzbapi.entity.requestBean.AddSvReq;
import com.lcxw.hxzbapi.entity.requestBean.DeleteSvCommentReq;
import com.lcxw.hxzbapi.entity.requestBean.GetAllSvCommentReq;
import com.lcxw.hxzbapi.entity.requestBean.GetSvListReq;
import com.lcxw.hxzbapi.entity.requestBean.LikeSvComment;
import com.lcxw.hxzbapi.entity.requestBean.LikeSvReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.entity.response.SvCommentResp;
import com.lcxw.hxzbapi.entity.response.SvListResp;
import com.lcxw.hxzbapi.service.ShortVideoCommentService;
import com.lcxw.hxzbapi.service.ShortVideoService;
import com.lcxw.hxzbapi.service.SvCommentLikeService;
import com.lcxw.hxzbapi.service.SvLikeService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbcommon.common.DateUtils;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ShortVideoRealization {

    @Resource
    ShortVideoService shortVideoService;

    @Resource
    ShortVideoCommentService shortVideoCommentService;

    @Resource
    YwpdMemberService memberService;

    @Resource
    SvLikeService svLikeService;

    @Resource
    SvCommentLikeService svCommentLikeService;

    //获取短视频列表
    Response getSvList(GetSvListReq requestBody) {
        Long userId = null;
        //用户没有登录修改
        if (!StringUtil.isEmpty(requestBody.getUserId())) {
            userId = Long.parseLong(requestBody.getUserId());
        }
        int pageNum = requestBody.getPageNum();
        Integer pageSize = requestBody.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        List<SvListResp> data = shortVideoService.selectSvList(userId, pageNum, pageSize, false);
        return Response.ok(data);
    }

    //获取我发布的短视频列表
    public Response getMySvList(GetSvListReq requestBody) {
        long userId = Long.parseLong(requestBody.getUserId());
        int pageNum = requestBody.getPageNum();
        Integer pageSize = requestBody.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }

        List<SvListResp> data = shortVideoService.selectSvList(userId, pageNum, pageSize, true);
        return Response.ok(data);
    }


    //添加短视频
    Response addSv(AddSvReq addSvReq) {

        long userId = Long.parseLong(addSvReq.getUserId());
        ShortVideo shortVideo = new ShortVideo();
        shortVideo.setUserId(userId);
        shortVideo.setContent(addSvReq.getContent());
        shortVideo.setUrl(addSvReq.getUrl());
        shortVideo.setAddTime(StringUtil.getDateTime());
        shortVideo.setStatus(1);

        shortVideoService.insert(shortVideo);
        return Response.ok();
    }

    //删除短视频
    Response deleteSv(long userId, Integer svId) {
        ShortVideo shortVideo = shortVideoService.selectBySvId(svId);
        if (null == shortVideo) {
            return Response.error(ErrorCode.DATA_NOT_EXIST, "删除的记录短视频不存在，删除失败");
        } else if (shortVideo.getUserId() != userId) {
            return Response.error(ErrorCode.NO_PERMISSION, "该短视频无权删除，不是所有人");
        }
        shortVideoService.deleteBySvId(svId);
        return Response.ok();
    }

    //添加短视频评论
    Response addSvComment(AddSVCommentReq addSVCommentReq) {

        long fromUserId = Long.parseLong(addSVCommentReq.getUserId());
        long toUserId = Long.parseLong(addSVCommentReq.getToUserId());
        Integer svId = addSVCommentReq.getSvId();
        Integer parentId = addSVCommentReq.getParentId();
        String comment = addSVCommentReq.getComment();

        ShortVideoComment shortVideoComment = new ShortVideoComment();
        shortVideoComment.setSvId(svId);
        shortVideoComment.setComment(comment);
        shortVideoComment.setFromUserId(fromUserId);
        shortVideoComment.setToUserId(toUserId);
        shortVideoComment.setParentId(parentId);
        shortVideoComment.setAddTime(StringUtil.getDateTime());
        shortVideoComment.setStatus(1);

        shortVideoCommentService.insert(shortVideoComment);

        return Response.ok();
    }

    //获取短视频的所有评论
    Response getAllSvComment(GetAllSvCommentReq getAllSvCommentReq) {
        Integer svId = getAllSvCommentReq.getSvId();
        long userId = Long.parseLong(getAllSvCommentReq.getUserId());
        int pageNum = getAllSvCommentReq.getPageNum();
        Integer pageSize = getAllSvCommentReq.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }

        List<ShortVideoComment> shortVideoComments = shortVideoCommentService
                .selectCommentList(svId, pageNum, pageSize);
        List<SvCommentResp> data = new ArrayList<>();
        for (ShortVideoComment svComment : shortVideoComments
                ) {
            svComment
                    .setFromUserImgUrl(memberService.selectImgUrlByuserId(svComment.getFromUserId()));
            svComment
                    .setFromUserNickName(
                            memberService.selectNickNameByUserId(svComment.getFromUserId()));
            int isLike =
                    svCommentLikeService.getIsLike(svComment.getCommentId(), userId);
            svComment.setIsLike(isLike);
            svComment.setAddTime(DateUtils.getTerm(svComment.getAddTime()));

            SvCommentResp svCommentResp = new SvCommentResp();
            svCommentResp.setShortVideoComment(svComment);

            //通过comment查询改评论的二级评论
            Integer parentId = svComment.getCommentId();
            List<ShortVideoComment> subShortVideoComments = shortVideoCommentService
                    .selectSubCommentList(parentId);

            for (ShortVideoComment subSvComment : subShortVideoComments
                    ) {
                subSvComment
                        .setFromUserImgUrl(
                                memberService.selectImgUrlByuserId(subSvComment.getFromUserId()));
                subSvComment
                        .setFromUserNickName(
                                memberService.selectNickNameByUserId(subSvComment.getFromUserId()));
//                subSvComment
//                    .setToUserNickName(
//                        memberService.selectNickNameByUserId(subSvComment.getToUserId()));
                int isSubLike =
                        svCommentLikeService.getIsLike(subSvComment.getCommentId(), userId);
                subSvComment.setIsLike(isSubLike);
                subSvComment.setAddTime(DateUtils.getTerm(subSvComment.getAddTime()));

            }

            svCommentResp.setSubShortVideoComment(subShortVideoComments);

            data.add(svCommentResp);
        }
        return Response.ok(data);
    }

    //删除一条短视频的评论
    public Response deleteSvComment(DeleteSvCommentReq deleteSvCommentReq) {

        int commentId = deleteSvCommentReq.getCommentId();
        long userId = Long.parseLong(deleteSvCommentReq.getUserId());
        ShortVideoComment svComment = shortVideoCommentService.getOne(commentId);
        if (null == svComment) {
            return Response.error(ErrorCode.DATA_NOT_EXIST, "要删除的评论不存在");
        }
        if (userId != svComment.getFromUserId()) {
            return Response.error(ErrorCode.NO_PERMISSION, "无权限删除，不是该用户的评论");
        }
        shortVideoCommentService.delete(svComment);
        return Response.ok();

    }

    //短视频点赞
    Response likeSv(LikeSvReq likeSvReq) {

        long userId = Long.parseLong(likeSvReq.getUserId());
        int svId = likeSvReq.getSvId();
        SvLike svLike = new SvLike();
        svLike.setUserId(userId);
        svLike.setSvId(svId);
        svLikeService.insert(svLike);

        return Response.ok();
    }

    //取消短视频点赞
    Response unlikeSv(LikeSvReq likeSvReq) {
        long userId = Long.parseLong(likeSvReq.getUserId());
        int svId = likeSvReq.getSvId();
        SvLike svlike = svLikeService.getSvlike(svId, userId);
        if (svlike != null) {
            svLikeService.delete(svlike.getLikeId());
            return Response.ok();
        } else {
            return Response.error(ErrorCode.NO_PERMISSION, "该视频没有点赞");
        }
    }

    //短视频的评论点赞
    public Response likeSvComment(LikeSvComment likeSvComment) {
        long userId = Long.parseLong(likeSvComment.getUserId());
        Integer commentId = likeSvComment.getCommentId();
        SvCommentLike svCommentLike = new SvCommentLike();
        svCommentLike.setCommentId(commentId);
        svCommentLike.setUserId(userId);
        svCommentLikeService.insert(svCommentLike);

        ShortVideoComment shortVideoComment = shortVideoCommentService.getOne(commentId);
        shortVideoCommentService.giveLike(shortVideoComment);
        return Response.ok();
    }

    //取消短视频的评论点赞
    public Response unlikeSvComment(LikeSvComment likeSvComment) {
        long userId = Long.parseLong(likeSvComment.getUserId());
        int commentId = likeSvComment.getCommentId();
        SvCommentLike svCommentLike = svCommentLikeService.selectOne(commentId, userId);

        if (svCommentLike != null) {
            svCommentLikeService.delete(svCommentLike.getLikeId());
            shortVideoCommentService.unGiveLike(shortVideoCommentService.getOne(commentId));
            return Response.ok();
        } else {
            return Response.error(ErrorCode.NO_PERMISSION, "该条评论没有点赞");
        }

    }


    public Response getOneSv(int svId) {
        ShortVideo shortVideo = shortVideoService.selectBySvId(svId);
        Long userId = shortVideo.getUserId();
        YwpdMember ywpdMember = memberService.selectYwpdMemberByUserId(userId);
        JSONObject data = new JSONObject();
        int commentNum = shortVideoCommentService.getCommentNum(svId);
        int likes = svLikeService.getLikes(svId);
        data.put("imgUrl", ywpdMember.getImgUrl());
        data.put("commentNum", commentNum);
        data.put("isLike", 0);
        data.put("likes", likes);
        data.put("nickName", ywpdMember.getNickName());
        data.put("shortVideo", shortVideo);
        return Response.ok(data);

    }
}
